<?php include "menu.phtml"; ?>
<div id="right">
	<div class="rightbox">
		<div class="context"><div class="in"><a href="javascript:void(0);" id="context_menu" class="icon_link the_down"><span></span></a></div></div>
		<h1><?php echo $this->translate->_("Edit Shot Record"); ?></h1>
		<br />
		<?php echo $this->form; ?>
	</div>
	<div class="rightbox" style="padding: 0px; border: 0px;">
		<?php if(isset($_SERVER['HTTP_REFERER'])): ?>
			<div class="left"><input onclick="Petolio.go('<?php echo $_SERVER['HTTP_REFERER']; ?>');" type="button" value="<?php echo $this->translate->_("< Back to List"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
			<div class="clear"></div>
		<?php else: ?>
			<div class="left"><input onclick="Petolio.go('<?php echo $this->url(array('controller'=>'shot', 'action'=>'view', 'id'=> $this->record->getId()), 'default', true) ?>');" type="button" value="<?php echo $this->translate->_("< Back to List"); ?>" id="submit" name="prev" style="margin: 0px;"><div class="clear"></div></div>
			<div class="clear"></div>
		<?php endif; ?>
	</div>
</div>
<style type="text/css">
	img.cancel {
		display: block;
		float: left;
		margin: 4px;
		cursor: pointer;
	}
</style>
<script type="text/javascript">
	var Transform = function() {
		var store = {
			sickness: <?php echo $this->sickness; ?>,
			translate: ['<?php echo $this->translate->_('Other'); ?>']

		// bind stuff
		}, bind = function() {
			// no sickness? just stick with the original input then
			if(!store.sickness.length > 0)
				return;

			// assemble the options
			var opt = null,
				val = $('input[name="sickness"]').val(),
				found = false;

			$.each(store.sickness, function(k, v){
				if(val == v[0])
					found = true;

				var sel = val == v[0] ? 'selected="selected"' : '';
				opt += '<option value="'+ v[0] +'" '+ sel +'>'+ v[1] +'</option>';
			});
			opt += '<option value="0">'+ store.translate[0] +'</option>';

			// replace input with select
			$('img.cancel').remove();
			$('input[name="sickness"]').replaceWith(
				'<select class="chzn-select" name="sickness" id="sickness" rel="#sickness-select">'+
					opt+
				'</select>'
			);

			// not found? swap :)
			if(!found && $(this).attr('class') != 'cancel')
				swap(val);

			Petolio.handleSelects();

		}, swap = function(ignore) {
			var value = '';

			// smart things happening in here lol
			if(typeof ignore == 'object') {
				if($(this).val() != 0)
					return;
			} else value = ignore;

			$('#sickness_chzn').remove();
			$('select[name="sickness"]').replaceWith(
				'<input type="text" name="sickness" id="sickness" value="'+ value +'" rel="#sickness-text">'+
				'<img src="/images/icons/delete.png" class="cancel" />'
			);

		// listen for events
		}, listen = function() {
			$("div.rightbox").delegate('select[name="sickness"]', 'change', swap);
			$("div.rightbox").delegate('select[name="sickness"]', 'keyup', swap);
			$("div.rightbox").delegate('img.cancel', 'click', bind);

		// public, yay
		}, __construct = function() {
			bind();
			listen();
		};

		return {
			init: __construct
		};
	}();

	READY(Transform.init);
</script>